package org.eenie.appseed

import android.content.Context
import android.content.Intent
import android.os.Environment
import app.rive.runtime.kotlin.core.Rive
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.EncryptUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoostDelegate
import com.idlefish.flutterboost.FlutterBoostRouteOptions
import com.idlefish.flutterboost.containers.FlutterBoostActivity
import com.nobody.api.cache.ApiCache
import io.flutter.embedding.android.FlutterActivityLaunchConfigs
import io.flutter.embedding.engine.FlutterEngine
import org.eenie.appseed.ui.boost.VpFlutterActivity
import org.eenie.appseed.ui.form.FormActivity
import org.eenie.core.base.BaseApplication
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import xcrash.ICrashCallback
import xcrash.XCrash
import xcrash.XCrash.InitParameters
import java.io.File


class SeedApp : BaseApplication(), ICrashCallback {


    override fun onCreate() {
        super.onCreate()



        Utils.init(this)

        ARouter.openDebug()
        ARouter.openLog()
        ARouter.init(this)


        startKoin {
            androidLogger()
            androidContext(this@SeedApp)
        }




        Rive.init(this)

        initFlutterBoost()

        ApiCache.init(externalCacheDir ?: cacheDir)


    }


    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)

        val params = InitParameters() // 设置软件版本号
            .setAppVersion(BuildConfig.VERSION_NAME)
            .setJavaRethrow(false)
            .setJavaCallback(this)

            .setNativeRethrow(false)
            .setNativeCallback(this)

            .setAnrRethrow(true)
            .setAnrCallback(this)

        XCrash.init(
            this, params
        )

    }

    override fun onCrash(logPath: String, emergency: String) {

    }


    private fun initFlutterBoost() {
        FlutterBoost.instance().setup(this, object : FlutterBoostDelegate {
            override fun pushNativeRoute(options: FlutterBoostRouteOptions) {
                LogUtils.e(options.pageName())
                //这里根据options.pageName来判断你想跳转哪个页面，这里简单给一个
                val intent = Intent(FlutterBoost.instance().currentActivity(), FormActivity::class.java)
                FlutterBoost.instance().currentActivity().startActivityForResult(intent, options.requestCode())


            }

            override fun pushFlutterRoute(options: FlutterBoostRouteOptions) {
                LogUtils.e(options.pageName())
                val intent: Intent = FlutterBoostActivity.CachedEngineIntentBuilder(VpFlutterActivity::class.java)
                    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.opaque)
                    .destroyEngineWithActivity(false)
                    .uniqueId(options.uniqueId())
                    .url(options.pageName())
                    .urlParams(options.arguments())
                    .build(FlutterBoost.instance().currentActivity())
                FlutterBoost.instance().currentActivity().startActivity(intent)
            }
        }) { engine: FlutterEngine ->


        }

    }


}